package com.ruoyi.until;

import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Component
public class RabbitmqUtil {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(queues = "hospitalQueue")
    public void send(String msg, Channel channel, Message message) throws IOException {

        try {
            String username = (String)redisTemplate.opsForValue().get("username:" + msg);

            if(StringUtils.isEmpty(username)){
                System.out.println("mq已经消费");
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),true,false);
                return;
            };

            System.out.println("发送成功 "+new Date()+" "+username+":预约成功");

            redisTemplate.delete("username:"+msg);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);

        } catch (IOException e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),true,false);
            throw new RuntimeException(e);
        }
    }

}
